package com.ktjy.service.impl;

import com.ktjy.entity.Account;
import com.ktjy.entity.Bank;
import com.ktjy.entity.Page;
import com.ktjy.mapper.BankMapper;
import com.ktjy.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;  // 修改为Spring的事务注解

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("BankService")
public class BankServiceImpl implements BankService {
    @Autowired
    private BankMapper bankMapper;

    @Override
    public Account login(Account account) {
        return bankMapper.login(account);
    }

    @Override
    public Page findPageVo(Bank bank, String cardno, String startTime, String endTime, Integer pageNo, Integer pageSize) {
        System.out.println("Service层接收参数: cardno=" + cardno + ", startTime=" + startTime + ", endTime=" + endTime);
        
        Page page = new Page();
        // 设置分页参数
        page.setRows(pageSize);
        page.setTotalPage(pageNo);
        
        try {
            // 查询总记录数
            Integer count = bankMapper.count(cardno, startTime, endTime);
            System.out.println("查询到的总记录数: " + count);
            page.setTotal(count != null ? count : 0);
            
            // 计算偏移量
            int offset = (pageNo - 1) * pageSize;
            
            // 查询数据列表
            List<Bank> list = bankMapper.page(offset, startTime, endTime, pageSize, cardno);
            System.out.println("查询到的记录列表: " + (list != null ? list.size() : "null"));
            page.setList(list);
            
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Service层异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)  // 修改事务注解，明确指定回滚条件
    public Map<String, Object> transfer(String fromCardno, String toCardno, double amount, String remark) {
        Map<String,Object> rest = new HashMap<>();
        if (amount<=0){
            rest.put("success",false);
            rest.put("message","转账金额必须大于零");
            return rest;
        }
        if (fromCardno.equals(toCardno)){
            rest.put("success",false);
            rest.put("message","不可向自己转账");
            return rest;
        }
        Account account=bankMapper.checkBalance(fromCardno);
        if (account==null){
            rest.put("success",false);
            rest.put("message","转出账户不存在");
            return rest;
        }
        if (account.getBalance()<amount){
            rest.put("success",false);
            rest.put("message","余额不足");
            return rest;
        }
        Account toaccount=bankMapper.checkBalance(toCardno);
        if (toaccount==null){
            rest.put("success",false);
            rest.put("message","没有该账户");
            return rest;
        }
        // 处理备注信息
        if (remark == null || remark.trim().isEmpty()) {
            // 为转出方设置默认备注
            remark = "转给" + toCardno + "的转账";
        }

        Date now = new Date();
        // 执行转账操作
        bankMapper.transferIn(amount, toCardno);
        bankMapper.transferOut(amount, fromCardno);
        
        // 记录转出方交易记录
        Bank outRecord = new Bank();
        outRecord.setCardno(fromCardno);
        outRecord.setTransactionDate(now);
        outRecord.setAmount(amount);
        outRecord.setBalance(account.getBalance() - amount);
        outRecord.setTransactionType("转出");
        outRecord.setRemark(remark);  // 使用处理后的备注
        bankMapper.transferRecord(outRecord);
        // 记录转入方交易记录
        Bank inRecord = new Bank();
        inRecord.setCardno(toCardno);
        inRecord.setTransactionDate(now);
        inRecord.setAmount(amount);
        inRecord.setBalance(toaccount.getBalance() + amount);
        inRecord.setTransactionType("转入");
        inRecord.setRemark("来自" + fromCardno + "的转账");  // 为转入方设置对应的备注
        bankMapper.transferRecord(inRecord);

        rest.put("success", true);
        rest.put("message", "转账成功");
        return rest;
    }

    @Override
    public Account checkBalance(String cardno) {
        return bankMapper.checkBalance(cardno);
    }

    @Override
    public int confirmPassword(Account account) {
        return bankMapper.confirmPassword(account);
    }

    @Override
    public int changePassword(Account account) {
        return bankMapper.changePassword(account);
    }
}
